﻿using System;
using System.Collections.Generic;
using System.Linq;
using VoiceAd.Models;

namespace VoiceAd.DAL
{
    public class ContentRepository : IContentRepository, IDisposable
    {
        private dbVoiceAdDataContext context = new dbVoiceAdDataContext();
        public ContentRepository(dbVoiceAdDataContext db)
        {
            this.context = db;
        }

        public IEnumerable<tblContent> GetListContent()
        {
            return context.tblContents.ToList();
        }

        public tblContent GetContentById(int id)
        {
            return context.tblContents.Where(x => x.Id == id).FirstOrDefault();
        }

        public void InsContent(tblContent tblContent)
        {
            context.tblContents.InsertOnSubmit(tblContent);
            context.SubmitChanges();
        }

        public void UpdContent(tblContent tblContent)
        {
            tblContent obj = context.tblContents.Where(x => x.Id == tblContent.Id).First();
            obj.Title_VI = tblContent.Title_VI;
            obj.Title_EN = tblContent.Title_EN;
            obj.Body_VI = tblContent.Body_VI;
            obj.Body_EN = tblContent.Body_EN;
            context.SubmitChanges();
        }

        public void DelContent(int id)
        {
            tblContent obj = context.tblContents.Single(x => x.Id == id);
            context.tblContents.DeleteOnSubmit(obj);
            context.SubmitChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}